/**
 * 格式化时间
 * @param {Date/Number} date 秒时间戳或者date
 * @param {String} format 默认格式：yyyy-MM-dd HH:mm:ss
 * @returns 
 */
export function formatDate(date, format) {
  if (date == undefined || date == 0)
    return "";
  if ((date + "").length < 11 && (date + "").length > 3)
    date = date * 1000;
  date || (date = new Date());
  (date instanceof Date) || (date = new Date(date));
  var cfg = {
    yyyy: date.getFullYear(),
    yy: date.getFullYear().toString().substring(2),
    MM: date.getMonth() + 1,
    dd: date.getDate(),
    HH: date.getHours(),
    hh: date.getHours() < 12 ? "AM " + date.getHours() : "PM " + (date.getHours() - 12),
    mm: date.getMinutes(),
    ss: date.getSeconds()
  }
  for (var e in cfg) {
    cfg[e] = cfg[e].toString().replace(/^(\d)$/, "0$1");
  }
  format || (format = "yyyy-MM-dd HH:mm:ss");
  return format.replace(/([a-z])(\1)*/ig, function (m) {
    return cfg[m];
  });
}


